package me.leiusual.sharing.ui.order

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations.map
import androidx.lifecycle.Transformations.switchMap
import androidx.lifecycle.ViewModel
import androidx.paging.PagedList
import me.leiusual.sharing.api.model.orderlist.Content
import me.leiusual.sharing.api.model.orderlist.OrderList
import me.leiusual.sharing.repository.OrderRepository
import me.leiusual.sharing.repository.model.Data
import javax.inject.Inject

/**
 * author: leiusual
 * create on: 2019-07-28
 * description:
 */
class OrderListViewModel @Inject constructor(
    private val orderRepository: OrderRepository
) : ViewModel() {

  // 分类列表

  private val _orderList = MutableLiveData<ParamsOrderList>()
  private val _listing = map(_orderList) {
    orderRepository.orderList(it.storeId, "", it.status)
  }
  val loadInitial: LiveData<Data<OrderList>> = switchMap(_listing) {
    it.loadInitial
  }
  val orderList: LiveData<PagedList<Content>> = switchMap(_listing) {
    it.pagedList
  }

  fun orderList(storeId: Long, status: Int) {
    _orderList.value = ParamsOrderList(storeId, status)
  }

  data class ParamsOrderList(val storeId: Long, val status: Int)
}